#include <stdio.h>

int main(int argc, char *argv[])
{
	unsigned int lcnt, case_num = 1, i, flg;
	unsigned int r, k, n, a[1000], re, ptr, tcarry, head;

	scanf("%u", &lcnt);
	while (lcnt--) {
		re = 0;
		ptr = 0;
		head = 0;

		scanf("%u %u %u", &r, &k, &n);
		for (i=0; i<n; i++) {
			scanf("%u", &a[i]);
		}

		for (i=0; i<r; i++) {
			tcarry = 0; head = ptr % n; flg = 1;
			while (tcarry + a[ptr%n] <= k) {
				if (0 != tcarry && head == ptr % n) break;
				tcarry += a[ptr%n];
				ptr++;
			}
			re += tcarry;
		}

		printf("Case #%u: %u\n", case_num, re);

		case_num++;
	}

	return 0;
}
